package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或者套餐到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车的数据{}", shoppingCart);
        //设置用户的userid
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {//说明购物车中的是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else {//说明购物车的是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否已经在购物车
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);


        if (cartServiceOne != null) {
            // 如果在则shopping_cart表中的number数量对应相加即可,不存在的话则加入购物车，数量默认为1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
            } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
               }
        return R.success(cartServiceOne);
    }

    /**
     * 查询当前购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 清空当前购物车
     * @return
     */
   @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空成功");
   }
   @PostMapping("/sub")
    public R<String> delet(@RequestBody ShoppingCart shoppingCart){
       LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
       Long dishId = shoppingCart.getDishId();
       if (dishId!=null){
           lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
           ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
           Integer number = one.getNumber();
           one.setNumber(number-1);
           shoppingCartService.updateById(one);
           if (one.getNumber()==0){
               shoppingCartService.removeById(one);
           }
       }else {
           lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
           ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
           Integer number = one.getNumber();
           one.setNumber(number-1);
           shoppingCartService.updateById(one);
           if (one.getNumber()==0){
               shoppingCartService.removeById(one);
           }
       }

       return R.success("删除成功");
   }
}
